Interactive Graphics

Packages loaded for this talk

library(plotly)
library(leaflet)
library(ggiraph)
library(highcharter)
library(rCharts) # ramnathv/rCharts
library(ggplot2)
library(tibble)
library(dplyr)
library(tidyr)

Why Interactive Graphics?

  • Because it’s fun

  • Because it’s cool

  • Because it allows a new dimension of conveying results

  • Because a collaborator asked

(Some) Packages in R

  • plotly - especially ggplotly()

  • ggiraph

  • highcharter

  • Won’t cover

    • rCharts (ramnathv/rCharts)

    • rbokeh

    • leaflet

How to build

Many cases you know how to build a ggplot2 object

May want to “interactive” it up

Many times you will hit roadblocks or limitations

Scatterplot

We’re going to be using the toyest example in R, mtcars ! But we want the car names

cars = mtcars %>% 
    rownames_to_column(var = "car") %>% 
    separate(car, into = c("make", "blah"), sep = " ", remove = FALSE, extra = "merge") %>% 
    select(make, car, everything()) %>% 
    select(-blah) %>% 
    as_tibble()
cars
# A tibble: 32 × 13
   make  car     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 Mazda Mazd…  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2 Mazda Mazd…  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3 Dats… Dats…  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4 Horn… Horn…  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5 Horn… Horn…  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6 Vali… Vali…  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7 Dust… Dust…  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8 Merc  Merc…  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9 Merc  Merc…  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10 Merc  Merc…  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# ℹ 22 more rows

Scatterplot

Weight (in tons) vs miles per gallon (MPG)

(ggplot(cars, aes(wt, mpg)) +
    geom_point())

Scatterplot (a little better)

(p <- ggplot(cars, aes(wt, mpg)) +
    geom_point() +
    xlab("Weight (in tons)") + 
    ylab("Miles Per Gallon (MPG)") + 
    theme_bw(base_size = 22))

Interactive Points (plotly)

plotly::ggplotly turns gg objects to plotly objects!

ggplotly(p)

Interactive Points (plotly)

plotly::ggplotly turns your objects into plotly objects!

ggplotly(p + geom_line())

Customize Hover Over Things

plotly_button_config = function(fig,
                                buttons = c("toImage",
                                            "resetViews"),
                                displaylogo = FALSE,
                                displayModeBar = TRUE) {
  buttons = as.list(buttons)
  buttons = list(buttons)
  fig %>%
    plotly::config(modeBarButtons = buttons,
                   displaylogo = displaylogo,
                   displayModeBar = displayModeBar)
}

Interactive Points (plotly)

plotly_button_config(ggplotly(p))

Interactive Points (ggiraph)

In ggiraph (giraffe) you cannot transform a ggplot object but you have the same syntax, but put an _interactive on it.

Put a "Bird on It" Meme from Portlandia TV show

Interactive Points (ggiraph)

(g <- ggplot(cars, aes(wt, mpg)) +
    geom_point_interactive() +
    xlab("Weight (in tons)") + 
    ylab("Miles Per Gallon (MPG)") + 
    theme_bw(base_size = 22))

Need to girafe it?

girafe(ggobj = g)

ggiraph: No default tooltips

g <- ggplot(cars, aes(wt, mpg, tooltip = car)) +
    geom_point_interactive() +
    xlab("Weight (in tons)") + 
    ylab("Miles Per Gallon (MPG)") + 
    theme_bw(base_size = 22)
girafe(ggobj = g)

More Interaction!

g = ggplot(cars, aes(x = factor(cyl), y = hp)) + 
  geom_boxplot(outlier.shape = NA)
ggplotly(g)

Interaction works with colour

gcol = ggplot(cars, aes(x = hp, y = mpg, colour = factor(cyl))) + 
  geom_point() +
  guides(colour = "none") +
  geom_smooth(se = FALSE) 
ggplotly(gcol)

Interaction (still) works with facets

ggplotly(gcol + facet_wrap(~ factor(cyl)))

Highcharter: Click away groups

cars %>% 
  hchart(
    "scatter",
    hcaes(x = hp, y = mpg, group = factor(cyl)))

Making a quick frequency data set

car_count = cars %>% 
  mutate(producer = ifelse(as.logical(am), "America", "Not America"),
         cylinders = factor(cyl)) %>%   
    group_by(producer, cylinders) %>% 
  summarise(
    hp = round(mean(hp), 2),
    n = dplyr::n()) %>% 
  group_by(producer) %>% 
  mutate(pct = n/sum(n)) %>% 
  ungroup()

Highcharter

car_count %>% 
  hchart(
    "bar",
    hcaes(y = hp, x = producer, group = cylinders)
  )

Highcharter Tile Map:

Highcharter may come at a (literal) price

Highcharts offers both a commercial license as well as a free non-commercial license

Pricing Chart for Highcharts

rCharts: Different Syntax

n1 <- car_count %>% 
  nPlot(n ~ producer, group = "cylinders", data = ., type = "multiBarChart")
n1$show('inline', include_assets = TRUE, cdn = TRUE)

rCharts: Scale to 100%

n2 <- car_count %>% 
  nPlot(pct ~ producer, group = "cylinders", data = ., type = "multiBarChart")
n2$show('inline', include_assets = TRUE, cdn = TRUE)

Making Connected Graphs are Hard